"""
@author hezhiwei
@date 2022年11月15日 17点14分
@description 用于过滤掉文件中指定内容的应用, 主要功能: 处理相关的文件, 如`.gitiattributes`,`.gitignore`
"""

"""
实现思路:
1. 创建文件.gitattributes
2. 读取.gitinore追加内容,#gg_begin .gitattributes #gg_end
3. 全局配置 | 本地仓库配置
"""

from pathlib import Path
import os

# window_keep_time_s = 30 # 窗口保持时间

# generate_slogan = "The following content is generated from GitDeepIngore"

def process1():
    """ 处理gitattributes """
    file = Path('./.gitattributes')
    file_types = ['xml', 'java', 'js'
    , 'vue', 'html', 'py','c','cpp' ]
    with open(file, 'a+', encoding='utf-8') as f:
        with open(file, 'r', encoding='utf-8') as f_read:
            content = f_read.read()
        # f.write(f'#{generate_slogan}\n')
        for file_type in file_types:
            if file_type not in content:
                f.write(f'*.{file_type}   filter=gitignore\n')     
            
def process2():
    """ gitignore 追加内容 """
    file = Path('./.gitignore')
    with open(file, 'a+', encoding='utf-8') as f: # 且不存在 自动创建文件
        with open(file, 'r', encoding='utf-8') as f_read:
            content = f_read.read()
        if 'git-deep-ignore' not in content:
            
            f.write("\n".join([
                "\n#gg_begin",
                "#It's better not to modify the part that is generated by GitDeepIngore!\n"
                "/.gitattributes",
                "/git-deep-ignore*.exe",
                "#gg_end"
            ]))

def checkGitPrepare():
    """ 检查Git是否可运行 """
    result = os.system("git  branch")
    if result != 0:
        result = os.system('git init')
        return result == 0
    return True


def print_other_msg():
    print(
"""
==========应用信息===========
@product-name git-deep-ignore  
@author 何志伟    
@email 1498408920@qq.com    
@version 1.0
@publish-date 2022年11月16日
=============================
"""
        )

def print_tip_msg():
    print(
"""
-----------------相关提示------------------

1. 重复多次运行此程序, 将不造成任何影响, 程序能自行进行相关处理.

2. 请确保该程序是在项目的根目录的下运行

3. 具体使用方法, 请查看文档, 或者联系作者

-----------------------------------------------
"""
        )

def process3():
    """ 通过命令行本地配置git """
    import time
    result_msg = ""
    if checkGitPrepare():
        os.system(""" git config filter.gitignore.clean "sed '/#gg_begin/,/#gg_end$/d;/#gg_line/d;/\/\/#gg_begin/,/\/\/#gg_end$/d;/\/\/#gg_line/d;/gg_begin\s\{0,\}-->/,/gg_end\s\{0,\}-->$/d'" """)
        os.system(""" git config filter.gitignore.smudge cat  """)
        print("自动配置中... ")
        os.system(""" 
        git config --get filter.gitignore.clean
        git config --get filter.gitignore.smudge
        """)
        project_name = Path.cwd().name
        result_msg = \
        "自动配置完成!\n" + \
         f"git-deep-ignore应用到项目({project_name})中成功!"
    else:
        result_msg = "配置失败, 请先检查环境, 环境中没有Git"
    os.system('clear') == 0 or os.system('cls')
    print_other_msg()
    print("\n")
    print("===========执行结果==========")
    print(result_msg)
    print("=============================")
    print_tip_msg()
    print(f'\n按任意键可退出该窗口...\n')
    input()
    # time.sleep(window_keep_time_s)


def process():
    process1()
    process2()
    process3()

if __name__ == "__main__":
    process()
    pass



